package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dao.OrdersDao;
import com.itheima.entity.AddressBook;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.AddressBookService;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王磊
 * @date 2022/5/11
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders>implements OrdersService {

    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 提交订单
     * @param orders
     * 页面就传了支付方式
     * paymethoad 1
     * remark 备注
     * 地址id
     */
    @Override
    public void subimt(Orders orders) {
        //生成全局唯一id作为订单号
        long id = IdWorker.getId();
        //配送地址id,要根据这个id查询具体的地址数据
        Long addressBookId = orders.getAddressBookId();
        //这是地址数据
        AddressBook addressBook = addressBookService.getById(addressBookId);
        //设置订单的id
        //   orders.setId(id);
        //设置订单号
        orders.setNumber(String.valueOf(id));
        //设置用户ID
        orders.setUserId(BaseContext.getCurrentId());
        //设置状态,1为未付款
        orders.setStatus(1);
        //设置下单时间
        orders.setCheckoutTime(LocalDateTime.now());
        //设置手机号
        orders.setPhone(addressBook.getPhone());
        //设置地址
        orders.setAddress(addressBook.getDetail());
        //设置用户名
        orders.setUserName(BaseContext.getCurrentId().toString());
        //设置收货人
        orders.setConsignee(addressBook.getConsignee());
        orders.setOrderTime(LocalDateTime.now());
        //设置实收金额 菜品/套餐 数量*单价
        BigDecimal bigDecimal = new BigDecimal(0);
        //首先查询当前用户的购物车数据
        List<ShoppingCart> list = shoppingCartService.list();
        for (ShoppingCart cart : list) {
            Integer number = cart.getNumber();
            BigDecimal decimal = new BigDecimal(number);
            BigDecimal money = cart.getAmount();
            decimal.multiply(money);
           bigDecimal=decimal.add(decimal);
        }
        orders.setAmount(bigDecimal);
        //保存基本数据到订单表
        this.save(orders);
        //保存订单详情到订单详情表
        //购物车数据里面有菜名或者套餐名,图片,用户id,菜品id/套餐id,口味,数量,单价
       List<OrderDetail> OrderDetaillist= list.stream().map((item)->{
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(item.getName());
            orderDetail.setOrderId(id);
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setNumber(item.getNumber());
            orderDetail.setAmount(item.getAmount());
             BigDecimal big = new BigDecimal(item.getNumber());
           orderDetail.setAmount(big.multiply(item.getAmount()));

            orderDetail.setImage(item.getImage());
            return orderDetail;
        }).collect(Collectors.toList());

        orderDetailService.saveBatch(OrderDetaillist);
    }
}
